﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Bpms.Bll;
using Bpms.Cache;
using Bpms.Common;
using Bpms.Control;
using Bpms.Log;

namespace Web.projectManagement.category
{
    public partial class index : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.leftCtrl.CurrentColumn = ColumnType.ProjectManagement;
            BusinessCategory.DBChanged += () => CacheContext.GetCacheService(new DefaultCache()).RemoveObject(CacheKey.BusinessCategoryList.ToString());
            OperationLogAdapter log = new OperationLogAdapter();

            if (!Page.IsPostBack)
            {
                string action = Utility.GetQueryString("action");
                string catHtml = string.Empty;

                if (action == "delete")
                {
                    int id = Utility.GetQueryInt("id");
                    switch (BusinessCategory.Delete(id))
                    {
                        case BusinessCategory.BusinessCategoryDeleteResult.删除成功:
                            catHtml = BusinessCategory.GetTreeHTML(0);
                            Response.Write(catHtml);
                            break;
                        case BusinessCategory.BusinessCategoryDeleteResult.本类别下含有子类别:
                            Response.Write("-1");
                            break;
                        case BusinessCategory.BusinessCategoryDeleteResult.根类别下只剩最后一个类别:
                            Response.Write("-2");
                            break;
                        case BusinessCategory.BusinessCategoryDeleteResult.删除失败:
                            Response.Write("0");
                            break;
                        default:
                            Response.Write("0");
                            break;
                    }

                    Response.End();
                    return;
                }
                if (action == "setNumber")
                {
                    int id = Utility.GetQueryInt("id");
                    int number = Utility.GetQueryInt("number");
                    int isSetSuccess = BusinessCategory.SetSortNumber(id, number);

                    if (isSetSuccess != 1)
                    {
                        Response.Write(isSetSuccess.ToString());
                    }
                    else
                    {
                        catHtml = BusinessCategory.GetTreeHTML(0);
                        Response.Write(catHtml);
                    }

                    Response.End();
                    return;
                }

                this.ltlCatHTML.Text = BusinessCategory.GetTreeHTML(0); ;
            }

        }

        protected void ClearCache(object sender, EventArgs e)
        {
            CacheContext.GetCacheService(new DefaultCache()).RemoveObject(CacheKey.BusinessCategoryList.ToString());
        }
    }
}
